<?php

namespace Api\Member\AuthManager\Models;

use Api\ModelExtTrait;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\SoftDeletes;

/**
 * 模型
 */
class Manager extends Model
{
    use SoftDeletes;
    use ModelExtTrait;

    /**
     * The table associated with the model.
     *
     * @var string
     */
    protected $table = 'member__auth_managers';

    /**
     * The attributes that should be mutated to dates.
     *
     * @var array
     */
    protected $dates = ['confine_time'];

    /**
     * 数据校验规则
     * @var array
     */
    public $rules = [
        'account' => [
            'required' => '请输入您的账号',
            'unique:member__auth_managers' => '账号已被占用',
            'methods' => 'regexAccount',
        ],
        'mobile' => [
            'methods' => 'regexMobile',
        ]
    ];

    /**
     * 强制字段类型
     * The attributes that should be cast to native types.
     *
     * @var array
     */
    protected $casts = [];
    
    /**
     * 序列化输出白名单
     * The attributes that should be visible in serialization.
     *
     * @var array
     */
    protected $visible = [
        'id',
        'account',
        'country_code',
        'mobile',
        'last_ip',
        'status',
        'roles',
        'auth_fail_num',
        'confine_time',
        'created_at',
        'updated_at',
    ];
    
/*
|--------------------------------------------------------------------------
| 访问器
|--------------------------------------------------------------------------
*/
    /**
     * 手机
     * mobile
     * @return string
     */
    // public function getMobileAttribute($value)
    // {
    //     if (! is_null($value)) {
    //         return mix_mobile($value);
    //     }
    // }

/*
|--------------------------------------------------------------------------
| 调整器
|--------------------------------------------------------------------------
*/

/*
|--------------------------------------------------------------------------
| 拓展方法
|--------------------------------------------------------------------------
*/
    /**
     * 禁用
     * @return void
     */
    public function forbidden()
    {
        $this->status = 2;
        $this->saveOrFail();
    }

    /**
     * 启用
     * @return void
     */
    public function invoke()
    {
        $this->status = 1;
        $this->saveOrFail();
    }

    
    /**
     * 判断账户是否被禁用
     * @return void
     */
    public function isForbidden()
    {
        return $this->status === 2;
    }

/*
|--------------------------------------------------------------------------
| 拓展校验方法
|--------------------------------------------------------------------------
*/
    public function regexAccount($value)
    {
        $pattern = '/^([a-z0-9_]){6,16}$/';
        if (preg_match($pattern, $value) === 0) {
            return '请设置 6 ~ 16 位的小写字母作为账号（可加入数字与下划线）';
        }
    }

    public function regexMobile($value)
    {

        if (!$value) {
            return '手机号不能为空';
        }

        $pattern = '/(^\d{6,10}$)|((1[3|4|5|6|7|8|9])+\d{9}$)/';
        if (preg_match($pattern, $value) === 0) {
            return '手机号格式错误';
        }

        $info = $this->where('mobile', $value)->first();
        if ($info) {
            return '手机号已被注册';
        }

    }


}
